/**判断是否为空*/
const isEmpty = obj => {
	if (Array.isArray(obj)) {
		return obj.length === 0;
	} else if (Object.prototype.toString.call(obj) === '[object Object]') {
		return Object.keys(obj).length === 0;
	} else {
		return !obj;
	}
};
/**
 * 拼接查询语句
 * @param {Object} keyMap 查询的条件
 * @param {String} sql 查询语句
 * @param {Boolean} isLike 是否模糊查询
 * @param {Object} pageParams 分页参数
 * */
const getSql = (keyMap, sql, isLike, pageParams) => {
	let keyArr = [];
	let valueArr = [];
	Object.keys(keyMap).forEach(key => {
		if (keyMap[key]) {
			keyArr.push(key);
			valueArr.push(isLike ? `${keyMap[key]}%` : keyMap[key]);
		}
	});
	if (keyArr.length > 0) {
		sql += ' where ';
		keyArr.forEach((key, index) => {
			sql += `${index === 0 ? '' : ' and '}${key}${isLike ? ' like ' : ' = '}?`;
		});
	}
	if (!isEmpty(pageParams)) {
		let { pageNo, pageSize } = pageParams;
		sql += ` limit ${(pageNo - 1) * pageSize},${pageSize}`;
	}
	return { sql, value: valueArr };
};
module.exports = {
	isEmpty,
	getSql
};
